package com.donica.wamstest.Controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.donica.wamstest.entity.AirfleetVO;
import com.donica.wamstest.entity.AirlineVO;
import com.donica.wamstest.entity.AirplaneVO;
import com.donica.wamstest.entity.CommonFindVO;
import com.donica.wamstest.entity.DeviceFindVO;
import com.donica.wamstest.entity.DeviceInfoFindVO;
import com.donica.wamstest.entity.DeviceInfoVO;
import com.donica.wamstest.entity.DeviceVO;
import com.donica.wamstest.entity.ReturnMessage;
import com.donica.wamstest.service.DeviceManageService;
import com.donica.wamstest.service.LogService;
import com.donica.wamstest.utils.LogUtil;

import io.swagger.annotations.Api;

//中文
@RestController
@RequestMapping("/api/v1/")
@Api(value = "DeviceController", tags = "设备控制器")
public class DeviceController {

	@Autowired
	private DeviceManageService deviceService;

	@Autowired
    private LogService logService;
	
	// 测试
	@RequestMapping(value = "hello", method = RequestMethod.GET)
	public String hello() {
		return "Hello SpringBoot!";
	}

	// 添加机队
	@RequestMapping(value = "airfleets", method = RequestMethod.POST)
	public ReturnMessage InsertAirfleet(@RequestBody List<AirfleetVO> volist) {
		ReturnMessage rtnMsg = new ReturnMessage();
		try {

			long count = deviceService.InsertAirfleet(volist);
			rtnMsg.setResult(count);
		} catch (Exception e) {
			rtnMsg.setCode(ReturnMessage.FAIL);
			rtnMsg.setMsg(e.getMessage());
		}

		logService.SaveLog(LogUtil.ACTION_ADD_AIRFLEET, rtnMsg.getCode(), rtnMsg.getMsg(),volist);
		return rtnMsg;
	}

	// 删除机队
	@RequestMapping(value = "airfleets", method = RequestMethod.DELETE)
	public ReturnMessage DeleteAirfleet(@RequestBody List<AirfleetVO> volist) {
		ReturnMessage rtnMsg = new ReturnMessage();
		try {

			long count = deviceService.deleteAirfleet(volist);
			rtnMsg.setResult(count);
		} catch (Exception e) {
			rtnMsg.setCode(ReturnMessage.FAIL);
			rtnMsg.setMsg(e.getMessage());
		}

		logService.SaveLog(LogUtil.ACTION_DELETE_AIRFLEET, rtnMsg.getCode(),rtnMsg.getMsg(), volist);
		return rtnMsg;
	}

	// 查询机队
	@RequestMapping(value = "airfleets/list/{pageSize}/{currPage}", method = RequestMethod.POST)
	public ReturnMessage searchAirfleet(@PathVariable("pageSize") int pageSize, @PathVariable("currPage") int currPage,
			@RequestBody CommonFindVO vo) {
		int firstIndex = pageSize * currPage;
		vo.setFirstIndex(firstIndex);
		vo.setPageSize(pageSize);
		vo.setCurrPage(currPage);
		return deviceService.searchAirfleet(vo);
	}

	// 添加飞机
	@RequestMapping(value = "airplanes", method = RequestMethod.POST)
	public ReturnMessage InsertAirplane(@RequestBody List<AirplaneVO> volist) {
		ReturnMessage rtnMsg = new ReturnMessage();
		try {

			long count = deviceService.InsertAirplane(volist);
			rtnMsg.setResult(count);
		} catch (Exception e) {
			rtnMsg.setCode(ReturnMessage.FAIL);
			rtnMsg.setMsg(e.getMessage());
		}

		logService.SaveLog(LogUtil.ACTION_ADD_AIRPLANE, rtnMsg.getCode(),rtnMsg.getMsg(), volist);
		return rtnMsg;
	}

	@RequestMapping(value = "airplanes", method = RequestMethod.PUT)
	public ReturnMessage UpdateAirplan(@RequestBody List<AirplaneVO> volist) {
		ReturnMessage rtnMsg = new ReturnMessage();
		try {

			long count = deviceService.UpdateAirplane(volist);
			rtnMsg.setResult(count);
		} catch (Exception e) {
			rtnMsg.setCode(ReturnMessage.FAIL);
			rtnMsg.setMsg(e.getMessage());
		}
		
		logService.SaveLog(LogUtil.ACTION_UPDATE_AIRPLANE, rtnMsg.getCode(), rtnMsg.getMsg(),volist);
		return rtnMsg;
	}

	// 删除飞机
	@RequestMapping(value = "airplanes", method = RequestMethod.DELETE)
	public ReturnMessage DeleteAirplane(@RequestBody List<AirplaneVO> volist) {
		ReturnMessage rtnMsg = new ReturnMessage();
		try {

			long count = deviceService.deleteAirplane(volist);
			rtnMsg.setResult(count);
		} catch (Exception e) {
			rtnMsg.setCode(ReturnMessage.FAIL);
			rtnMsg.setMsg(e.getMessage());
		}

		logService.SaveLog(LogUtil.ACTION_DELETE_AIRPLANE, rtnMsg.getCode(),rtnMsg.getMsg(), volist);
		return rtnMsg;
	}

	// 查询飞机
	@RequestMapping(value = "airplanes/list/{pageSize}/{currPage}", method = RequestMethod.POST)
	public ReturnMessage searchAirplane(@PathVariable("pageSize") int pageSize, @PathVariable("currPage") int currPage,
			@RequestBody CommonFindVO vo) {
		int firstIndex = pageSize * currPage;
		vo.setFirstIndex(firstIndex);
		vo.setPageSize(pageSize);
		vo.setCurrPage(currPage);
		return deviceService.searchAirplane(vo);
	}

	// 查询航空公司列表
	@RequestMapping(value = "airlines", method = RequestMethod.POST)
	public ReturnMessage searchAirlines(@RequestBody AirlineVO vo) {
		ReturnMessage rtnMsg = new ReturnMessage();
		try {

			List<AirlineVO> result = deviceService.searchAirlines(vo);
			rtnMsg.setResult(result);
		} catch (Exception e) {
			rtnMsg.setCode(ReturnMessage.FAIL);
			rtnMsg.setMsg(e.getMessage());
		}

		return rtnMsg;
	}

	// ----设备操作-----
	// 添加设备
	@RequestMapping(value = "devices", method = RequestMethod.POST)
	public ReturnMessage InsertDevice(@RequestBody List<DeviceVO> volist) {
		ReturnMessage rtnMsg = new ReturnMessage();
		try {

			long count = deviceService.InsertDevice(volist);
			rtnMsg.setResult(count);
		} catch (Exception e) {
			rtnMsg.setCode(ReturnMessage.FAIL);
			rtnMsg.setMsg(e.getMessage());
		}

		logService.SaveLog(LogUtil.ACTION_ADD_DEVICE, rtnMsg.getCode(),rtnMsg.getMsg(), volist);
		return rtnMsg;
	}

	@RequestMapping(value = "devices", method = RequestMethod.PUT)
	public ReturnMessage UpdateDevice(@RequestBody List<DeviceVO> volist) {
		ReturnMessage rtnMsg = new ReturnMessage();
		try {

			long count = deviceService.UpdateDevice(volist);
			rtnMsg.setResult(count);
		} catch (Exception e) {
			rtnMsg.setCode(ReturnMessage.FAIL);
			rtnMsg.setMsg(e.getMessage());
		}
		logService.SaveLog(LogUtil.ACTION_UPDATE_DEVICE, rtnMsg.getCode(), rtnMsg.getMsg(),volist);
		return rtnMsg;
	}

	@RequestMapping(value = "devices", method = RequestMethod.DELETE)
	public ReturnMessage DeleteDevice(@RequestBody List<DeviceVO> volist) {
		ReturnMessage rtnMsg = new ReturnMessage();
		try {

			long count = deviceService.deleteDevice(volist);
			rtnMsg.setResult(count);
		} catch (Exception e) {
			rtnMsg.setCode(ReturnMessage.FAIL);
			rtnMsg.setMsg(e.getMessage());
		}

		logService.SaveLog(LogUtil.ACTION_DELETE_DEVICE, rtnMsg.getCode(), rtnMsg.getMsg(),volist);
		return rtnMsg;
	}

	@RequestMapping(value = "devices/list/{pageSize}/{currPage}", method = RequestMethod.POST)
	public ReturnMessage searchDevice(@PathVariable("pageSize") int pageSize, @PathVariable("currPage") int currPage,
			@RequestBody DeviceFindVO vo) {
		int firstIndex = pageSize * currPage;
		vo.setFirstIndex(firstIndex);
		vo.setPageSize(pageSize);
		vo.setCurrPage(currPage);
		return deviceService.searchDevice(vo);
	}
	
	//设备信息操作
	@RequestMapping(value = "deviceInfos", method = RequestMethod.POST)
	public ReturnMessage SetDeviceInfo(@RequestBody List<DeviceInfoVO> volist) {
		ReturnMessage rtnMsg = new ReturnMessage();
		try {

			long count = deviceService.InsertDeviceInfo(volist);
			rtnMsg.setResult(count);
		} catch (Exception e) {
			rtnMsg.setCode(ReturnMessage.FAIL);
			rtnMsg.setMsg(e.getMessage());
		}

		return rtnMsg;
	}


	@RequestMapping(value = "deviceInfos", method = RequestMethod.DELETE)
	public ReturnMessage DeleteDeviceInfo(@RequestBody List<DeviceInfoVO> volist) {
		ReturnMessage rtnMsg = new ReturnMessage();
		try {

			long count = deviceService.deleteDeviceInfo(volist);
			rtnMsg.setResult(count);
		} catch (Exception e) {
			rtnMsg.setCode(ReturnMessage.FAIL);
			rtnMsg.setMsg(e.getMessage());
		}
		
		logService.SaveLog(LogUtil.ACTION_DELETE_DEVICEINFO, rtnMsg.getCode(),rtnMsg.getMsg(), volist);

		return rtnMsg;
	}

	@RequestMapping(value = "deviceInfos/list/{pageSize}/{currPage}", method = RequestMethod.POST)
	public ReturnMessage searchDeviceInfo(@PathVariable("pageSize") int pageSize, @PathVariable("currPage") int currPage,
			@RequestBody DeviceInfoFindVO vo) {
		int firstIndex = pageSize * currPage;
		vo.setFirstIndex(firstIndex);
		vo.setPageSize(pageSize);
		vo.setCurrPage(currPage);
		return deviceService.searchDeviceInfo(vo);
	}
	
}
